<?php

namespace System\Core;

if (!defined('BASEPATH')) die('Access Denied.');

/**
 * µMVC Framework (c) 2011.
 * 
 * PHP5 MVC Framework software for PHP 5.2.17 or later
 * 
 * @package         µMVC
 * @filename        system/core/frontcontroller.class.php
 * @copyright       Lanz (c) 2011.
 * 
 */

class FrontControllerException extends CommonException {};

/**
 * FrontController class
 * 
 * @package         µMVC
 * @subpackage      Core
 * @category        System
 */
class FrontController extends BaseSingleton
{

    /**
     * Router
     * @var object instance of Router class. 
     */
    private $_router;
    
    function __construct()
    {
        $this->_router = Router::instance();        
        Log::instance()->write(Log::DEBUG, 'FrontController Class intitialized');
    }

    /**
     * Main entrance to the entire MVC, runs the MVC system. Fetches all
     * important information and runs the requested controller.
     */
    public function run()
    {
        $controller = '\\System\\Controllers\\'.$this->_router->getController();
        $action = $this->_router->getAction();
                   
        if (file_exists(strtolower(BASEPATH.'controllers'.DS.$this->_router->getController().'.class.php')))
        {
            $c = new $controller();
            $c->{$action.'Action'}();
        }
        else
        {
            throw new FrontControllerException("Controller not found: ".BASEPATH.'controllers'.DS.$this->_router->getController().'.class.php');
        }
    }
    
    public function siteRedirect($page)
    {
        $baseUrl = Config::instance()->getItem('base_url');
        header("Location: $baseUrl$page");
        exit;
    }
    
    public function message($title, $message)
    {
         include BASEPATH.'errors'.DS.'message.php';
         exit;        
    }
    
    public function infoMsg($message)
    {
        $this->message('Information', $message);
    }
    
    public function warningMsg($message)
    {
         $this->message('Warning', $message);
    }

    public function errorMsg($message)
    {
        $this->message('Error', $message);
    }
}

?>
